package com.im.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.im.service.dto.BaseException;
import com.im.service.dto.MetePay;
import com.im.service.dto.PageQuery;
import com.im.service.metePay.smo.IMetePayOperSMO;
import com.im.service.metePay.smo.IMetePayQuerySMO;
import com.im.service.model.MetePayItemVo;
import com.im.util.ReturnsUtil;
import com.im.util.StringUtil;

import net.sf.json.JSONObject;

/**
 *
 * 计量支付信息 事务操作SMO
 * @author wangxh
 * 2018年4月1日
 */
@RestController
@RequestMapping("/metePayService")
public class MetePayService {

    private static final Logger logger = LogManager.getLogger(ProjectInfoService.class);

    @Autowired
    private IMetePayQuerySMO metePayQuerySMO;

    @Autowired
    private IMetePayOperSMO metePayOperSMO;
    /**
     *
     * 计量支付列表查询.
     *
     * @param jsonStr
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/queryMetePayList")
    public String queryMetePayList(@RequestBody String jsonStr){
        if (logger.isInfoEnabled()) {
            logger.info("请求入参json:{}", jsonStr);
        }

        try {
            if (jsonStr == null || jsonStr.length() <= 0) {
                throw new BaseException("请求入参为空，请核查数据!");
            }

			PageQuery<MetePayItemVo> result = metePayQuerySMO.queryMetePayList(jsonStr);
            return ReturnsUtil.returnSuccess(result).toString();
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error(ReturnsUtil.getStackTrackMessage(e));
            }
            return ReturnsUtil.returnFailure(e.getMessage()).toString();
        }
    }

	/**
	 *
	 * 计量支付汇总列表查询.
	 *
	 * @param jsonStr
	 * @return
	 * @throws Exception
	 */
	@PostMapping(value = "/queryMetePayStaticList")
	public String queryMetePayStaticList(@RequestBody String jsonStr) {
		if (logger.isInfoEnabled()) {
			logger.info("请求入参json:{}", jsonStr);
		}

		try {
			if (jsonStr == null || jsonStr.length() <= 0) {
				throw new BaseException("请求入参为空，请核查数据!");
			}

			PageQuery<MetePayItemVo> result = metePayQuerySMO.queryMetePayStaticList(jsonStr);
			return ReturnsUtil.returnSuccess(result).toString();
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.getMessage()).toString();
		}
	}
	
	/**
	 *
	 * 计量汇总--明细
	 *
	 * @param jsonStr
	 * @return
	 * @throws Exception
	 */
	@PostMapping(value = "/queryMetePayStaticDetailList")
	public String queryMetePayStaticDetailList(@RequestBody String jsonStr) {
		if (logger.isInfoEnabled()) {
			logger.info("请求入参json:{}", jsonStr);
		}

		try {
			if (StringUtil.isEmpty(jsonStr)) {
				throw new BaseException("请求入参为空，请核查数据!");
			}
			PageQuery<MetePayItemVo> result = metePayQuerySMO.queryMetePayStaticDetailList(jsonStr);
			return ReturnsUtil.returnSuccess(result).toString();
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.getMessage()).toString();
		}
	}

    /**
     *
     * 查询计量支付详情.
     *
     * @param
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/queryMetePayDetail")
    public String queryMetePayDetail(@RequestBody MetePay metePay){
        if (logger.isInfoEnabled()) {
            logger.info("请求入参:{}", metePay.toString());
        }
        try {
            if (metePay == null || StringUtil.isEmpty(metePay.getPayId())) {
                throw new BaseException("请求入参为空，请核查数据!");
            }
			MetePayItemVo result = metePayQuerySMO.queryMetePayDetail(metePay);
            return ReturnsUtil.returnSuccess(result).toString();
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error(ReturnsUtil.getStackTrackMessage(e));
            }
            return ReturnsUtil.returnFailure(e.getMessage()).toString();
        }
    }


    /**
     *
     *  新增计量支付信息
     * @param
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/addMetePay")
    public String addMetePay(@RequestBody MetePay metePay){
        if (logger.isInfoEnabled()) {
            logger.info("请求入参:{}", metePay.toString());
        }
        try {
            if (metePay == null) {
                throw new BaseException("请求入参为空，请核查数据!");
            }
            metePayOperSMO.addMetePay(metePay);
            return ReturnsUtil.returnSuccess(new JSONObject()).toString();
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error(ReturnsUtil.getStackTrackMessage(e));
            }
            return ReturnsUtil.returnFailure(e.getMessage()).toString();
        }
    }

    /**
     *
     *  修改计量支付信息
     * @param metePay
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/updateMetePay")
    public String updateMetePay(@RequestBody MetePay metePay){
        if (logger.isInfoEnabled()) {
            logger.info("请求入参:{}", metePay.toString());
        }
        try {
			if (metePay == null) {
                throw new BaseException("请求入参为空，请核查数据!");
            }
            metePayOperSMO.updateMetePay(metePay);
            return ReturnsUtil.returnSuccess(new JSONObject()).toString();
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error(ReturnsUtil.getStackTrackMessage(e));
            }
            return ReturnsUtil.returnFailure(e.getMessage()).toString();
        }
    }

    /**
     *
     *  删除计量支付信息
     * @param metePay
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/deleteMetePay")
    public String deleteMetePay(@RequestBody MetePay metePay){
        if (logger.isInfoEnabled()) {
            logger.info("请求入参:{}", metePay.toString());
        }
        try {
            if (metePay == null || StringUtil.isEmpty(metePay.getPayId())) {
                throw new BaseException("请求入参为空，请核查数据!");
            }
            metePayOperSMO.deleteMetePay(metePay);
            return ReturnsUtil.returnSuccess(new JSONObject()).toString();
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error(ReturnsUtil.getStackTrackMessage(e));
            }
            return ReturnsUtil.returnFailure(e.getMessage()).toString();
        }
    }

}
